package com.foreknow.demo18;

import javax.xml.crypto.Data;
import java.sql.*;
import java.util.jar.JarOutputStream;

/**
 * Jdbc
 */
public class DBUtil {
    public static void main(String[] args) {
        try {
            //1.加载驱动程序
            //如果是8.0以上的版本需要使用com.mysql.cj.jdbc.Driver
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            //8.0以上: jdbc:mysql://localhost:3306/basic2?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
            //8.0以下版本：jdbc:mysql://localhost:3306/basic2
           Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
           //获取statement对象
            Statement stmt =conn.createStatement();
            //查询
//          ResultSet rs = stmt.executeQuery("select * from emp");
    //            //完成DML(insert, delete,update)操作
//
//            conn.setAutoCommit(false);
//            int isRight=stmt.executeUpdate("update emp set ename ='任亮' where empno=1001");
//            conn.commit();
//            System.out.println(isRight);
//           ResultSet rs= stmt.executeQuery("select * from emp");
////            System.out.println(rs);
//          int sm=  stmt.executeUpdate("insert into emp(empno,ename,job)values(9999,'tom','11')");

//            System.out.println(sm);
           int ii= stmt.executeUpdate("update emp set ename='xxx',job='222'where empno=9999");
            System.out.println(ii);
          //获取结果集的数据 next()方法是将光标向下移动一行,获取当前行的数据
//          while (rs.next()){
//              int empno =rs.getInt("empno");
//              String ename=rs.getNString("ename");
//              String job = rs.getString("job");
//              Date hir = rs.getDate("hiredate");
//              System.out.println(empno+"   "+ename+"   "+"   "+job);
//          }

          conn.close();
          stmt.close();
//          rs.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
